package com.promote.common.properties.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.promote.common.properties.FilePropertiesMap;
import com.promote.common.properties.service.PropertyService;

/**
 * @Description:
 * @Package com.tarena.platform.property.service.impl
 * @File: RpcPropertyServiceImpl
 * @Version V1.0
 * @author: chenyao
 * @date: 2019-06-26 15:46
 * @Copyright: 2019 www.tedu.cn Inc. All rights reserved, Designed By
 *             www.tedu.cn 注意：本内容仅限于达内时代科技集团内部传阅，禁止外泄以及用于其他的商业目
 */
@Component
public class RpcPropertyServiceImpl implements PropertyService {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public Map<String, Map<String, String>> loadProperties() {
		try {
			log.info("启用fileLoad配置>>>>>>>> ");
			Map<String, String> fileLoadMap = FilePropertiesMap.cacheProperties("fileLoad");
			if (CollectionUtils.isEmpty(fileLoadMap)) {
				log.info("加载配置文件失败，fileLoadMap.properties 文件不存在！ !");
				return Collections.emptyMap();
			}
			Map<String, Map<String, String>> map = new HashMap<>();
			log.info("加载本地配置文件。。。");
			String rpcActive = fileLoadMap.get("rpc.active");
			if (StringUtils.isEmpty(rpcActive) || rpcActive.equals("${filters.env}")) {
				// rpcActive = "dev";
				// fileLoadMap.put("rpc.active", rpcActive.trim().toLowerCase());
				// rpcActive = rpcActive.trim().toLowerCase();
			}

			log.info("激活环境变量 {} !", rpcActive);
			map.put("rpc", fileLoadMap);
			Set<String> keySet = fileLoadMap.keySet();
			for (String keyStr : keySet) {
				// 这里是解析 rpc.properties的，以前的项目 这个属性配置的值太多，太长，不方便查看，
				// 现在修改为允许配置rpc.properties0，rpc.properties1，或者rpc.propertiesOne,rpc.propertiesTwo这样的形式
				// 便于查看
				if (StringUtils.isBlank(keyStr) && !"fileLoad.properties".startsWith(keyStr)) {
					// 没有配置信息
					continue;
				}
				String fileProperties = fileLoadMap.get(keyStr);
				if (StringUtils.isEmpty(fileProperties)) {
					continue;
				}
				String[] propertiesStr = fileProperties.split(",");
				for (String str : propertiesStr) {
					log.info("加载本地配置文件:" + str);
					if (str == null || str.isEmpty()) {
						continue;
					}
					str = str.trim();
					Map<String, String> strMap = FilePropertiesMap.cacheProperties(str);
					if (strMap == null || strMap.isEmpty()) {
						strMap = new HashMap<>();
					}
					Map<String, String> platformMap = null;
					if (StringUtils.isNotBlank(rpcActive)) {
						platformMap = FilePropertiesMap.cacheProperties("distribute/" + rpcActive + "/" + str);
						if (!CollectionUtils.isEmpty(strMap)) {
							strMap.putAll(platformMap);
						}
					}
					if (CollectionUtils.isEmpty(strMap)) {
						log.info("本地配置文件 {} 加载内容为空", str);
						continue;
					}
					map.put(str, strMap);
				}
				if (CollectionUtils.isEmpty(map)) {
					log.info("配置文件加载失败 ! ");
					return Collections.emptyMap();
				}
			}
			log.info("配置文件加载完成 ");
			return map;
		} catch (

		Exception e) {
			e.printStackTrace();
			log.info("load properties error:{}", e.getMessage());
		}
		return Collections.emptyMap();
	}
}
